﻿using SharpDX;

namespace MikadoShooter
{
    public class Vector4Shoot : Vector4Base
    {
        public Vector4 CurrentColor { get; private set; }
        public Vector4 NextColor { get; private set; }
        public Vector4 OrgStartPos { get; private set; }
        public Vector4 StartPos { get; private set; }
        public Vector4 Direction { get; private set; }

        public Vector4Shoot(int verticals, Vector4 startPos, Vector4 startClr, Vector4 nextClr) : base(verticals)
        {
            StartPos = startPos;
            OrgStartPos = startPos;

            CurrentColor = startClr;
            NextColor = nextClr;

            Reset(verticals, null);
        }

        public void Reset(int verticals, Vector4? resetColor)
        {
            if (resetColor.HasValue)
            {
                StartPos = OrgStartPos;
                CurrentColor = NextColor;
                NextColor = resetColor.Value;
            }

            for (int i = 0; i < Data.Length; i++)
            {
                Data[i] = ((i & 1) == 0) ? StartPos : CurrentColor;
            }
        }

        public void SwitchColor()
        {
            Vector4 tmp = CurrentColor;
            CurrentColor = NextColor;
            NextColor = tmp;
        }

        public void ItsAHit()
        {
            Vector4 newColor = Data[1] == StdColors.WhiteColor ? CurrentColor : StdColors.WhiteColor;

            for (int i = 1; i < Data.Length; i += 2)
            {
                Data[i] = newColor;
            }
        }

        public void Shoot(Vector4 endPos)
        {
            StartPos = OrgStartPos;
            Direction = endPos - StartPos;
            Direction.Normalize();
        }

        public void BounchWall(Vector2 newStart, Vector2 side)
        {
            if (Direction.Y < 0)
                side.Y = -1.0f;

            Bounch(newStart, side);
        }

        public void BounchUp(Vector2 newStart, Vector2 side)
        {
            if (Direction.X > 0)
                side.X = -1.0f;

            Bounch(newStart, side);
        }

        private void Bounch(Vector2 newStart,Vector2 side)
        {
            StartPos = new Vector4(newStart, OrgStartPos.Z,OrgStartPos.W);
            Direction = new Vector4(Vector2.Reflect(newStart, side), Direction.Z, Direction.W) - StartPos;
            Direction.Normalize();
        }
    }
}